package demo.messaging.client.core;

import java.io.Serializable;

import demo.messaging.common.AbstractMessage;

public class MessageEvent implements Serializable {

	public static enum EnumMessageEventType {

		loginSuccess, logoutSuccess,

		listUserUpdated,

		invitationSent,

		invitationReceived,

		acceptanceSent,

		acceptanceReceived,

		cancelSent,

		cancelReceived,

		busyMessageReceived,

		applicationDataSent,

		applicationDataReceived,

		rpcError,
	}

	private EnumMessageEventType type;

	private AbstractMessage message;

	private Throwable throwable;

	public MessageEvent() {
		super();
	}

	/**
	 * Contract an event 
	 * containing no data.
	 * 
	 * @param type
	 * @param message
	 */
	public MessageEvent(final EnumMessageEventType type) {
		super();
		this.type = type;
	}

	/**
	 * Contract an event for notifying that a message 
	 * is sent or received.
	 * 
	 * @param type
	 * @param message
	 */
	public MessageEvent(final EnumMessageEventType type,
			final AbstractMessage message) {
		super();
		this.type = type;
		this.message = message;
	}

	/**
	 * Construct an event for notifying error
	 * on message sending or receiving.
	 * 
	 * @param type
	 * @param throwable
	 */
	public MessageEvent(final EnumMessageEventType type,
			final AbstractMessage message, final Throwable throwable) {
		super();
		this.type = type;
		this.message = message;
		this.throwable = throwable;
	}

	/**
	 * Construct an event for notifying error.
	 * 
	 * @param type
	 * @param throwable
	 */
	public MessageEvent(final EnumMessageEventType type,
			final Throwable throwable) {
		super();
		this.type = type;
		this.throwable = throwable;
	}

	public <T extends AbstractMessage> T getMessage() {
		return (T) this.message;
	}

	public Throwable getThrowable() {
		return this.throwable;
	}

	public EnumMessageEventType getType() {
		return this.type;
	}
}
